package cn.edu.wfit.modules.infomanagement.service.impl;

import cn.edu.wfit.common.core.utils.DateUtils;
import cn.edu.wfit.modules.infomanagement.domain.TBus;
import cn.edu.wfit.modules.infomanagement.domain.TBusDate;
import cn.edu.wfit.modules.infomanagement.domain.TBusReservation;
import cn.edu.wfit.modules.infomanagement.domain.TBusSchedule;
import cn.edu.wfit.modules.infomanagement.dto.DateAndReservation;
import cn.edu.wfit.modules.infomanagement.dto.DriverAndSchedule;
import cn.edu.wfit.modules.infomanagement.mapper.TBusDateMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusReservationMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusScheduleMapper;
import cn.edu.wfit.modules.infomanagement.service.ITBusScheduleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 存储每日发车安排Service业务层处理
 *
 * @author zhanghao
 * @date 2025-11-07
 */
@Service
public class TBusScheduleServiceImpl implements ITBusScheduleService {
    @Autowired
    private TBusScheduleMapper tBusScheduleMapper;
    @Autowired
    private TBusMapper tBusMapper;
    @Autowired
    private TBusDateMapper tBusDateMapper;
    @Autowired
    private TBusReservationMapper tBusReservationMapper;

    /**
     * 查询存储每日发车安排
     *
     * @param id 存储每日发车安排主键
     * @return 存储每日发车安排
     */
    @Override
    public TBusSchedule selectTBusScheduleById(Long id) {
        return tBusScheduleMapper.selectTBusScheduleById(id);
    }

    /**
     * 查询存储每日发车安排列表
     *
     * @param tBusSchedule 存储每日发车安排
     * @return 存储每日发车安排
     */
    @Override
    public List<TBusSchedule> selectTBusScheduleList(TBusSchedule tBusSchedule) {
        return tBusScheduleMapper.selectTBusScheduleList(tBusSchedule);
    }

    /**
     * 新增存储每日发车安排
     *
     * @param tBusSchedule 存储每日发车安排
     * @return 结果
     */
    @Override
    public int insertTBusSchedule(TBusSchedule tBusSchedule) {
        tBusSchedule.setCreateTime(DateUtils.getNowDate());
        int i = tBusScheduleMapper.insertTBusSchedule(tBusSchedule);
        Long id = tBusSchedule.getId();
        System.out.println("主键回显：" + id);
        //如果新增了临时发车,则直接新增一条预约管理记录
        if (tBusSchedule.getDepartureDate() != null) {
            TBusDate tBusDate = new TBusDate();
            tBusDate.setScheduleId(id);
            tBusDate.setDepartureData(tBusSchedule.getDepartureDate());
            //根据busid获取荷载人数
            TBus tBus = tBusMapper.selectTBusById(tBusSchedule.getBusId());
            tBusDate.setMaxCapacit(tBus.getMaxCapacity());
            //新增预约管理记录
            int i1 = tBusDateMapper.insertTBusDate(tBusDate);
        }
        return i;
    }

    /**
     * 修改存储每日发车安排
     *
     * @param tBusSchedule 存储每日发车安排
     * @return 结果
     */
    @Override
    public int updateTBusSchedule(TBusSchedule tBusSchedule) {
        //如果修改了临时发车,则需要新增一条预约管理记录
        if (tBusSchedule.getDepartureDate() != null) {
            TBusDate tBusDate = new TBusDate();
            tBusDate.setScheduleId(tBusSchedule.getId());
            tBusDate.setDepartureData(tBusSchedule.getDepartureDate());
            //根据busid获取荷载人数
            TBus tBus = tBusMapper.selectTBusById(tBusSchedule.getBusId());
            tBusDate.setMaxCapacit(tBus.getMaxCapacity());
            //删除修改之前的预约管理记录
            tBusDateMapper.deleteTBusDateByScheduleId(tBusSchedule.getId());
            //新增预约管理记录
            int i1 = tBusDateMapper.insertTBusDate(tBusDate);


        }
        tBusSchedule.setUpdateTime(DateUtils.getNowDate());
        return tBusScheduleMapper.updateTBusSchedule(tBusSchedule);
    }

    /**
     * 批量删除存储每日发车安排
     *
     * @param ids 需要删除的存储每日发车安排主键
     * @return 结果
     */
    @Override
    public int deleteTBusScheduleByIds(Long[] ids) {
        return tBusScheduleMapper.deleteTBusScheduleByIds(ids);
    }

    /**
     * 删除存储每日发车安排信息
     *
     * @param id 存储每日发车安排主键
     * @return 结果
     */
    @Override
    public int deleteTBusScheduleById(Long id) {
        return tBusScheduleMapper.deleteTBusScheduleById(id);
    }

    @Override
    public List<DriverAndSchedule> selectTBusScheduleByDriverId(Long driverId) {
        //根据id获取司机的发车安排
        List<TBusSchedule> tBusSchedules = tBusScheduleMapper.selectTBusScheduleByDriverId(driverId);
        //遍历发车安排
        List<DriverAndSchedule> driverAndSchedules = new ArrayList<>();
        for (TBusSchedule tBusSchedule : tBusSchedules) {
            //新建一个司机和发车安排的对象
            DriverAndSchedule driverAndSchedule = new DriverAndSchedule();
            //使用BeanUtils将tBusSchedule的属性复制到driverAndSchedule
            BeanUtils.copyProperties(tBusSchedule, driverAndSchedule);
            //根据busId获取公交车的信息
            TBus tBus = tBusMapper.selectTBusById(tBusSchedule.getBusId());
            //将tBus添加到司机和发车安排的对象中
            driverAndSchedule.settBus(tBus);
            //根据scheduleId获取TBusDate表中的记录,根据status获取进行中的数据
            TBusDate tBusDate = new TBusDate();
            tBusDate.setStatus(1L);
            tBusDate.setScheduleId(tBusSchedule.getId());
            //查询TBusDate表中的记录
            List<TBusDate> tBusDateList = tBusDateMapper.selectTBusDateList(tBusDate);
            //如果有进行中的记录,则添加到司机和发车安排的对象中
            if (!tBusDateList.isEmpty()) {
                List<DateAndReservation> dateAndReservationList = new ArrayList<>();
                //遍历tBusDateList
                for (TBusDate tBusDate1 : tBusDateList) {
                    DateAndReservation dateAndReservation = new DateAndReservation();
                    BeanUtils.copyProperties(tBusDate1, dateAndReservation);
                    //根据tBusDateId获取TBusReservation表中的教师预约记录
                    TBusReservation tBusReservation = new TBusReservation();
                    tBusReservation.setDateId(tBusDate1.getId());
                    tBusReservation.setReservationStatus(1L);
                    List<TBusReservation> tBusReservationList = tBusReservationMapper.selectTBusReservationList(tBusReservation);
                    //将教师预约记录添加到司机和发车安排的对象中
                    dateAndReservation.settBusReservation(tBusReservationList);
                    //将dateAndReservation添加到司机和发车安排的对象中
                    dateAndReservationList.add(dateAndReservation);
                }
                //将dateAndReservationList添加到司机和发车安排的对象中
                driverAndSchedule.setDateAndReservation(dateAndReservationList);
            }
            //添加到司机和发车安排的列表中
            driverAndSchedules.add(driverAndSchedule);
        }

        return driverAndSchedules;
    }


}
